package com.chyer.logistics_interest.entity;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class DataCrawDemo {

    private static int row = 1;
    //main方法 ,主入口
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        //2021年
        downLoadYearData("2021");

        long end = System.currentTimeMillis();
        log.info("start = {}, end={},耗时={}", start, end, (end - start));
    }


    public static void downLoadYearData(String year) throws IOException{
        long start = System.currentTimeMillis();
        Workbook wb = new XSSFWorkbook(); //创建excle
        Sheet sheet = wb.createSheet(year);//创建sheet
        String t0 = "10c216b8ec0e4364807790582cadbc3e";
        String token = "bH5aoEBkgcz64hvZVOpcXHmfHsJ/iKP/BLmncpWACvcoN+1axigGAF2tDbNlPZPKv5uwjq12BGIbHWUGEeO7CNoYIuWFXmjAYVZtEmbTAF9McVO9iSQPjORY2ycdFeaY3wM54deEkdAjJ/eMukQ3HKhNHMyu6pARKPeKlUxA0Ek=";
        String firstHtml = doPost(0, token, t0, year);
        Document firstDoc = Jsoup.parse(firstHtml);

        Integer totalNum = getTotalPageNum(firstDoc);
        String t = getT(firstDoc);
        token = getRequestVerificationToken(firstDoc);
        List<StringBuilder> firstRecord = getRecordList(firstDoc);

        writeExcle(sheet,firstRecord,1);

        if(totalNum != null && totalNum > 0){
            int time = 2;
            for(int i =2 ; i< totalNum; i++){
                String html = doPost(i, token, t, year);
                Document doc = Jsoup.parse(html);
                t = getT(doc);
                List<StringBuilder>  temList = getRecordList(doc);
                writeExcle(sheet,temList,time);
                try {
                    // 休眠 5 秒钟
                    Random random = new Random();
                    int num = random.nextInt(5);
                    log.info("================ 随机睡眠{}秒", num);
                    Thread.sleep(num * 1000);
                } catch (InterruptedException e) {
                    // 处理异常
                    e.printStackTrace();
                }

                if(time % 2 == 0){ //每2页写一次
                    FileOutputStream fout=new FileOutputStream("D:\\personSelf\\equipmentDate.xlsx");
                    wb.write(fout);//Workbook提供了write的方法
                    fout.close();//将输出流关闭
                }

                time = time + 1;
            }
        }

        long end = System.currentTimeMillis();
        log.info("start = {}, end={},耗时={}", start, end, (end - start));
    }




    public static String doPost(Integer pageNum, String token, String t, String year) {
        String result = null;
        try {
            HttpClient httpClient = HttpClients.createDefault();
            HttpPost request = new HttpPost("http://222.143.21.233:2018/21To23/pub/GongShiSearch"); //对方的url

            request.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            request.setHeader("Cookie", "td_cookie=2112575280; ASP.NET_SessionId=uhot0lq2mj1y35jyzuqxjrjh; __RequestVerificationToken_LzIxVG8yMw__=xiWob5DvzNqLl5jFJaClzsXDxGJi6zIWSHrX7XTkQzc88ZGbudCIF30x/CNIHn7V1IDDL7lYgZVsR2/Lwk60P3lU8Y0A2BJTwLrej9UWwV4Z31a2LG65QaQJTTgqXp0+jl+ueJ1O8nT69hbXl4VrZAn7EcA7wLTx6w0IRt6jW/I=");

            Map<String, String> formParams = new HashMap<String, String>();
            //验证token 和上面的 Cookie 保持一致
            formParams.put("__RequestVerificationToken", token);
            formParams.put("JiJuLeiXing", "轮式拖拉机");
            formParams.put("JiJuLeiXingCode", "200101");
            formParams.put("YearNum", year);
            if(pageNum == 0){
                formParams.put("t", "10c216b8ec0e4364807790582cadbc3e");
            }else{
                formParams.put("t", t);
            }

            //页数
            if(pageNum != null && pageNum > 0){
                formParams.put("PageIndex",pageNum.toString());
            }

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();

            if (formParams != null) {
                Set<String> keySet = formParams.keySet();
                for (String key : keySet) {
                    nvps.add(new BasicNameValuePair(key, formParams.get(key)));
                }
            }

            request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
            HttpResponse response = httpClient.execute(request);

            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, "utf-8");

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    //获得总页数
    public static Integer getTotalPageNum(Document doc){
        Integer num = 0;

        Element pager = doc.getElementById("pager");
        Elements a2 = pager.getElementsContainingOwnText("尾页");
        String href = a2.first().attr("href");
        int equleIndex = href.indexOf("=");
        String tailNum = href.substring(equleIndex+1);
        if(tailNum != null && tailNum.length() > 0){
            num = Integer.valueOf(tailNum);
        }
        return num;
    }


    public static String getRequestVerificationToken(Document doc){
        String verificationToken = null;
        Elements inputs = doc.select("input[name=__RequestVerificationToken]");
        verificationToken = inputs.get(0).attributes().get("value");
        return verificationToken;
    }

    //获得总页数
    public static String getT(Document doc){
        String t = "";
        Elements scripts = doc.select("script");

        for (Element script : scripts) {
            String scriptContent = script.html();
            // 使用正则表达式匹配 t 的值
            Pattern pattern = Pattern.compile("t\\s*=\\s*'([^']*)'");
            Matcher matcher = pattern.matcher(scriptContent);
            if (matcher.find()) {
                String tValue = matcher.group(1);
                log.info("The value of t is: " + tValue);
                t = tValue;
                break;
            }
        }
        return t;
    }

    //获得每页数据的记录
    public static List<StringBuilder> getRecordList(Document doc){
        List<StringBuilder> result = new ArrayList<>();
        Element tableBody = doc.getElementById("list-pub");
        Elements trList = tableBody.children();
        if(trList != null && trList.size() > 0){
            for(int i = 0 ; i < trList.size() ; i++){
                Element tr = trList.get(i);
                StringBuilder sb = new StringBuilder();
                Elements tdList = tr.children();
                if(tdList != null && tdList.size() > 0){
                    for(Element td : tdList){
                        sb.append(td.text()).append("****");
                    }
                    result.add(sb);
                    log.info("------第{}行的记录 = {}",(i+1),sb.toString());
                }
            }
        }

        return result;
    }


    public static void writeExcle(Sheet sheet,List<StringBuilder> records,int time ) throws IOException {
        if(time == 1){
            String[] headArr = {"序号","县","所在乡(镇)","购机者姓名","机具品目","生产厂家","产品名称",
                    "购买机型","购买数量(台)","经销商","购机日期","单台销售价格(元)","单台中央补贴额(元)","总补贴额(元)","出厂编号","状态","是否超录申请"};
            Row row = sheet.createRow(0);
            for(int m =0 ; m< headArr.length ; m++){
                Cell mCell = row.createCell(m);
                mCell.setCellValue(headArr[m]);
            }
        }

        if(records != null && records.size() > 0){
            for(int n = 0; n<records.size();n++){
                String record = records.get(n).toString();
                String [] aArr = record.split("\\*\\*\\*\\*");

                Row bodyRow = sheet.createRow(row);
                for(int j = 0 ; j< aArr.length; j++){
                    Cell cell = bodyRow.createCell(j);
                    cell.setCellValue(aArr[j]);
                }
                row++;
            }
        }

    }
}
